﻿using Flurl.Http;
using OkFlurl;
using System;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form2 : Form
    {

        FlurlForm form = new FlurlForm();

        public Form2()
        {
            InitializeComponent();

            form.UploadBufferSize = 1024 * 4;
            form.DownloadBufferSize = 1024 * 4;

            form.UploadProgress = new FlurlProgress(data =>
            {
                //Console.WriteLine($"文件大小:{data.Total},当前已上传:{data.Read},==文件名{form.UploadProgress.FileName}");
                //Console.WriteLine("\r%{0:N4}", data.Percent * 100);

                //progressBar1.Value = Convert.ToInt32(data.Percent * 100);
                //label1.Text = (data.Percent * 100).ToString();

                label1.Text = data.Percent.ToString();
                progressBar1.Value = Convert.ToInt32(data.Percent);

            });

            form.DownloadProgress = new FlurlProgress(data =>
            {
                //Console.WriteLine($"文件大小:{data.Total},当前已下载传:{data.Read},==文件名{form.DownloadProgress.FileName}");
                //Console.WriteLine("\r%{0:N4}", data.Percent * 100);
                //label1.Text = (data.Percent * 100).ToString();
                //progressBar1.Value = Convert.ToInt32(data.Percent * 100);

                label1.Text = data.Percent.ToString();
                progressBar2.Value = Convert.ToInt32(data.Percent);

            });

            form.AddFile("a", @"D:\WPS_Setup_14309.exe");

            var par = new { id = "123", name = "李四123abcjadj" };

        }

        //开始请求
        private async void brn1_Click(object sender, EventArgs e)
        {
            var req2 = new FlurlRequest("http://localhost:5212/test/Upload111");
            var par = new { metadata = "123", name = "李四123abcjadj" };

            var filePath2 = @"C:\Users\Administrator\Desktop\dddd2.txt";

            //using var fs = new FileStream(filePath2, FileMode.Open);
            //form.AddStream("b", fs, "你好.txt", fs.Length);

            var bytes = Encoding.UTF8.GetBytes("上传的文件内容abc123");
            form.AddBytes("b", bytes, "内存ab12.txt");

            await FlurlExt.FormDownloadDiskAsync(req2, par, @"C:\\AA2", null, form);
            MessageBox.Show("好了OK");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            form.UploadProgress.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            form.UploadProgress.Stop();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            form.DownloadProgress.Start();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            form.DownloadProgress.Stop();
        }
    }
}
